import 'package:flutter/material.dart';

class GestureDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        // 设置状态栏的字体颜色
        // Brightness.light 黑色字体
        // Brightness.dark 白色字体
        brightness: Brightness.dark,
        title: Text('手势处理'),
      ),
      body: // MyButton()
          GestureDetector(
        onTapDown: (details) {
          ScaffoldMessenger.of(context)
              .showSnackBar(SnackBar(content: Text('按下按钮')));
        },
        onTapUp: (details) {
          ScaffoldMessenger.of(context)
              .showSnackBar(SnackBar(content: Text('松开按钮')));
        },
        onTap: () {
          ScaffoldMessenger.of(context)
              .showSnackBar(SnackBar(content: Text('点击按钮')));
        },
        child: Center(
            child: Container(
                padding: const EdgeInsets.all(12.0),
                decoration: BoxDecoration(
                    color: Theme.of(context).buttonColor,
                    borderRadius: BorderRadius.circular(5.0)),
                child: Text('测试按钮'))),
      ),
//          Builder(
//              builder: (context) => GestureDetector(
//                    onTapDown: (details) {
//                      Scaffold.of(context)
//                          .showSnackBar(SnackBar(content: Text('按下按钮')));
//                    },
//                    onTapUp: (details) {
//                      Scaffold.of(context)
//                          .showSnackBar(SnackBar(content: Text('松开按钮')));
//                    },
//                    onTap: () {
//                      Scaffold.of(context)
//                          .showSnackBar(SnackBar(content: Text('点击按钮')));
//                    },
//                    child: Center(
//                        child: Container(
//                            padding: const EdgeInsets.all(12.0),
//                            decoration: BoxDecoration(
//                                color: Theme.of(context).buttonColor,
//                                borderRadius: BorderRadius.circular(5.0)),
//                            child: Text('测试按钮'))),
//                  )),
    );
  }
}

// 当BuildContext在Scaffold之前时，调用Scaffold.of(context)会报错，
// 因为该context不是它自己的，而是父组件的，
// 这时可以通过Builder Widget来解决

class MyButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: (details) {
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('按下按钮')));
        // ignore: deprecated_member_use
        // Scaffold.of(context).showSnackBar(SnackBar(content: Text('按下按钮')));
      },
      onTapUp: (details) {
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('松开按钮')));
        // ignore: deprecated_member_use
        // Scaffold.of(context).showSnackBar(SnackBar(content: Text('松开按钮')));
      },
      onTap: () {
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('点击按钮')));
        // ignore: deprecated_member_use
        // Scaffold.of(context).showSnackBar(SnackBar(content: Text('点击按钮')));
      },
      child: Center(
        child: Container(
            padding: const EdgeInsets.all(12.0),
            decoration: BoxDecoration(
                color: Theme.of(context).buttonColor,
                borderRadius: BorderRadius.circular(5.0)),
            child: Text('测试按钮')),
      ),
    );
  }
}
